#import "DDFS.h"
#import "DDFSModel.h"
@interface DDFS()
@property (nonatomic, strong) CCLabel *leftLBL;
@property (nonatomic, strong) CCLabel *rightLBL;
@end
@implementation DDFS
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.leftLBL.text = @"支付选择";
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (CCLabel *)leftLBL
{
    if (!_leftLBL)
    {
        _leftLBL = [[CCLabel alloc] init];
        _leftLBL.font = FONT(UIFontWeightRegular, 15);
        _leftLBL.textColor = TEXT_BLACK_COLOR;
        [self addSubview:_leftLBL];
        _leftLBL.c_left.equal(self).with.coffset(GAP);
        _leftLBL.c_top.equal(self).with.coffset(GAP);
    }
    return _leftLBL;
}
- (CCLabel *)rightLBL
{
    if (!_rightLBL)
    {
        _rightLBL = [[CCLabel alloc] init];
        _rightLBL.font = FONT(UIFontWeightRegular, 15);
        _rightLBL.textColor = helpermanger().getPara().color;
        [self addSubview:_rightLBL];
        _rightLBL.c_right.equal(self).with.coffset(-GAP);
        _rightLBL.c_top.equal(self).with.coffset(GAP);
    }
    return _rightLBL;
}
DDFSManager fsManager()
{
    DDFSManager manger =
    {
        _getPara,
        _setPara,
        _currentIndex,
        _destroy
    };
    return manger;
};
static DDFSPara _para;
static DDFSPara _getPara()
{
    return _para;
}
static void _setPara(DDFS *view, DDFSPara para)
{
    _para = para;
    if (view)
    {
        view.rightLBL.text = para.jiage;
        if ([[view subviews] count] <= 2)
        {
            setupSubView(view);
        }
    }
}
static void (_currentIndex)(DDFS *view, int currentIndex)
{
    if(_para.currentIndex != currentIndex)
    {
        _para.currentIndex = currentIndex;
        [view performSelector:@selector(yjd_actionForZFFS:) withObject:[view viewWithTag:currentIndex + 200]];
    }
}
DDFSPara _destroy(DDFSPara para)
{
    para.jiage = @"";
    para.fs = nil;
    para.block = nil;
    para.currentIndex = 0;
    _para = para;
    return para;
};
static void setupSubView(DDFS *view)
{
    for (int i = 0; i <_para.fs.count; i++)
    {
        DDFSItem *fsView = [[DDFSItem alloc] init];
        DDFSModelPara fsModelPara;
        
        //        NSData *data = self.fs[i];
        ////        [data getBytes:&fsModelPara length:sizeof(fsModelPara)];
        //        fsItemManager().setPara(fsView,fsModelPara);
        
        NSDictionary *dict = _para.fs[i];
        fsModelPara.ID = [dict valueForKey:@"id"];
        fsModelPara.icon = [dict valueForKey:@"icon"];
        fsModelPara.name = [dict valueForKey:@"name"];
        fsView.tag = 200 + [fsModelPara.ID intValue];
        fsItemManager().setPara(fsView,fsModelPara);
        
        [fsView addTarget:view action:@selector(yjd_actionForZFFS:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:fsView];
        fsView.c_top.equal(view).with.coffset(JTWFSItemHEIGHT * (i+1));
        fsView.c_left.equal(view).with.coffset(GAP);
        fsView.c_right.equal(view).with.coffset(-GAP);
        fsView.c_height.equal(JTWFSItemHEIGHT);
    }
}
//- (void)setFs:(NSMutableArray *)fs
//{
//    if (_fs != fs)
//    {
//        _fs = fs;
//        [self setupSubView];
//    }
//}
//- (void)setJiage:(NSString *)jiage
//{
//    if (_jiage != jiage)
//    {
//        _jiage = jiage;
//        self.rightLBL.text = _jiage;
//    }
//}
//- (void)setupSubView
//{
//    for (int i = 0; i <self.fs.count; i++)
//    {
//        DDFSItem *fsView = [[DDFSItem alloc] init];
//        DDFSModelPara fsModelPara;
//        
////        NSData *data = self.fs[i];
//////        [data getBytes:&fsModelPara length:sizeof(fsModelPara)];
////        fsItemManager().setPara(fsView,fsModelPara);
//        
//        NSDictionary *dict = self.fs[i];
//        fsModelPara.ID = [dict valueForKey:@"id"];
//        fsModelPara.icon = [dict valueForKey:@"icon"];
//        fsModelPara.name = [dict valueForKey:@"name"];
//        fsView.tag = 200 + [fsModelPara.ID intValue];
//                fsItemManager().setPara(fsView,fsModelPara);
//
//        [fsView addTarget:self action:@selector(yjd_actionForZFFS:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:fsView];
//        fsView.c_top.equal(self).with.coffset(JTWFSItemHEIGHT * (i+1));
//        fsView.c_left.equal(self).with.coffset(GAP);
//        fsView.c_right.equal(self).with.coffset(-GAP);
//        fsView.c_height.equal(JTWFSItemHEIGHT);
//    }
//}
- (void)yjd_actionForZFFS:(DDFSItem *)sender
{
    for (UIView *view in [self subviews])
    {
        if (view.tag > 200)
        {
            DDFSItem *fsView = (DDFSItem *)view;
            fsView.selected = (fsView.tag == sender.tag)?YES:NO;
        }
    }
    _para.currentIndex = (sender)?((int)sender.tag - 200):0;
    if (_para.block)
    {
        _para.block();
    }
    //    if (self.delegate  && [self.delegate respondsToSelector:@selector(yjd_actionForJTWFSDelegate)])
    //    {
    //        [self.delegate yjd_actionForJTWFSDelegate];
    //    }
}
- (void)yjd_ergodic:(NSInteger )tag
{
    for (UIView *view in [self subviews])
    {
        if (view.tag > 200)
        {
            DDFSItem *fsView = (DDFSItem *)view;
            fsView.selected = (view.tag == tag)?YES:NO;
        }
    }
}
//- (void)setCurrentIndex:(int)currentIndex
//{
//    if (_currentIndex != currentIndex)
//    {
//        _currentIndex = currentIndex;
//        [self performSelector:@selector(yjd_actionForZFFS:) withObject:[self viewWithTag:_currentIndex + 200]];
//    }
//}

@end
